package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description DishController
 * @Author liuqiang
 * @Date 2024/2/19
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId") //保持缓存和数据库的数据一致，从Redis中删除缓存名称为dishCache::categoryId
    public Result insertDish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品:{}",dishDTO);
        dishService.insertDishAndFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 分页查询菜品信息
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("分页查询菜品信息")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品信息:{}",dishPageQueryDTO);
        PageResult pageResult= dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品信息
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品信息")
    @DeleteMapping
    @CacheEvict(cacheNames = "dishCache",allEntries = true) //当删除菜品信息时，有可能删除多个分类下的菜品信息，直接删除全部缓存
    public Result deleteDishList(@RequestParam List<Long> ids){
        log.info("批量删除菜品信息,ids:{}",ids);
        dishService.deleteDishList(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品信息")
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id){
        log.info("根据id查询菜品信息:{}",id);
        DishVO dishVO = dishService.getDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息和对应的口味信息
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品信息和对应的口味信息")
    @PutMapping
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品信息和对应的口味信息 dishDTO:{}",dishDTO);
        dishService.updateDishWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 根据categoryId和name查询菜品信息
     * @param categoryId
     * @param name
     * @return
     */
    @ApiOperation("根据categoryId和name查询菜品信息")
    @GetMapping("/list")
    public Result<List<Dish>> getDishByCategoryId(Long categoryId,String name){
        List<Dish> dishList = dishService.getDishByCategoryIdAndName(categoryId,name);
        return Result.success(dishList);
    }

    /**
     * 菜品起售、停售
     * @param status
     * @return
     */
    @ApiOperation("菜品起售、停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result updateeDishStatus(@PathVariable Integer status,Long id){
        log.info("菜品起售、停售：status={},id={}",status,id);
        dishService.updateDishStatus(status,id);

        return Result.success();
    }

}
